$(document).ready(function() {

	// send email check request

	$('#username-login').blur(function() {
		var email = $(this).val();
		$.ajax({
			url : 'index.php?page=index',
			type : 'POST',
			data : {
				email : email
			},

			success : function(response) {
				alert(response);
			}
		});
	});

	// send login request

	$('#login-button').on('click',function() {
		var email = $('#username-login').val();
		var password = $('#password-login').val();
		$.ajax({
			url : 'index.php?page=index',
			type : 'POST',
			data : {
				action : 'Login',
				email : email,
				password : password
			},

			success : function(response) {
				document.write(response);
			}
		});

	});

	$('#login').click(function() {

		$.ajax({
			url : 'logreg.php',
			type : 'GET',
			data : {
				par : 'login'
			},

			success : function(response) {
				$('#content-login-reg').html(response);
				$('#reg').css("text-decoration", "none");
				$("#login").css("text-decoration", "underline");
			}
		});
	});

	// send email registration check request
	$("#reg").on("click", (function() {

		$.ajax({
			url : 'logreg.php',
			type : 'GET',
			data : {
				par : 'reg'
			},

			success : function(response) {
				$('#content-login-reg').html(response);
				
				// send email  request for registration validation
				$('#email-reg').on("focusout",function(){
					$.ajax({
						url : 'index.php?page=index',
						type : 'POST' ,
						data : {
							emailreg : $('#email-reg').val(),
							page : 'index'
						},
						
						success : function(response){
							alert(response);
						}
					});
				});
				//send username request for registration validation
				
				$('#username-reg').on("focusout",function(){
					$.ajax({
						url : 'index.php?page=index',
						type : 'POST',
						data : {
							username : $('#username-reg').val()
						},
						
						success : function(response){
							alert(response);
						}
					});
				})
					
					
				
				
				//send password request for registration validation
				$('#password-reg').on("focusout",function(){
					$.ajax({
						url : 'index.php?page=index',
						type : 'POST',
						
						data : {
							password : $('#password-reg').val()
						},
						
						success : function(response){
							alert(response);
							
						}
					
					})
				});
				
				
				$('#reg-button').on("click",function(){
					$.ajax({
						url : 'index.php?page=index',
						type : 'POST' , 
						data : {
							action : 'Register',
							emailreg : $('#email-reg').val(),
							username : $('#username-reg').val(),
							password : $('#password-reg').val(),
							passwordRepeat : $('#password-repeat-reg').val()
						},
						
					success : function(response){
						alert(response);
					}
					});
				});
				
				
				$('#reg').css("text-decoration", "underline");
				$("#login").css("text-decoration", "none");

			}

		});

	}));
});
